import useUserStore from "@/store/modules/user";

/**
 * @desc v-hasRole 角色权限处理
 * @desc 使用 v-hasRole="['admin', 'user]"
 */
function checkRole(el, binding) {
  const userStore = useUserStore();
  const { value } = binding;
  const super_admin = "role_admin";
  if (value && Array.isArray(value) && value.length) {
    const roleValues = value;
    const hasRole = userStore.roles.some(role => {
      return super_admin === role || roleValues.includes(role);
    });
    if (!hasRole) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  } else {
    throw new Error(`need role! Like v-hasRole="['admin','user']"`);
  }
}

const directive = {
  mounted(el, binding) {
    checkRole(el, binding);
  },
  updated(el, binding) {
    checkRole(el, binding);
  }
};

export default directive;
